package com.haipumi.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.haipumi.beans.haipumi_skin;
import com.haipumi.beans.haipumi_user;
import com.haipumi.dao.DBHelper;
import com.haipumi.dao.IUserInfoDao;
import com.haipumi.utils.Encrypt;
import com.haipumi.utils.RequestUtil;

public class UserInfoDaoImpl implements IUserInfoDao {

	DBHelper db = new DBHelper();

	@Override
	public haipumi_user login(haipumi_user user) {
		//String sql=" select * from haipumi_user left join haipumi_skin on haipumi_user.sid=haipumi_skin.sid where ulogon=? and upwd=? and ustatus!=2 ";
		String sql="select * from (select simage,scolor,sstatus,uid,ulogon,upwd ,ualais,uimage,usex,uname,uqq,ucname,ucpro,uclass,utel,uinfo,udatetime,ustatus,uwealth ,urank ,uglory	,uvip,ulike	,uhabit	,ushieldid from haipumi_user u left join haipumi_skin s on u.sid=s.sid where ulogon=? and upwd=? and ustatus!=2 ) userdata ,(select count(*) as issuecount from haipumi_task where uid= (select uid from haipumi_user where ulogon=? and upwd=? ) ) issue ,(select count(*) as acceptcount from haipumi_task where taccid= (select uid from haipumi_user where ulogon=? and upwd=? ) ) accept ,(select sid from haipumi_user where ulogon=? and upwd=? ) sid";
		List<Object> params=new ArrayList<Object>();
		String md5pwd=Encrypt.md5(user.getUpwd());
		params.add(user.getUlogon());
		params.add(md5pwd);
		params.add(user.getUlogon());
		params.add(md5pwd);
		params.add(user.getUlogon());
		params.add(md5pwd);
		params.add(user.getUlogon());
		params.add(md5pwd);
		haipumi_user backuser=null;
		List<haipumi_user>list=null;
		try {
			list=db.finds(sql, params, haipumi_user.class);
			if(!list.isEmpty()){
				String ulike=list.get(0).getUlike();
				if(!"".equals( ulike )){//获取关注的任务数
					String[] ulikes=ulike.split(",");
					list.get(0).setUlike(Integer.toString( ulikes.length ));
				}else{
					list.get(0).setUlike("0");
				}
				
				backuser=list.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return backuser;
	}

	@Override
	public boolean addUser(haipumi_user user) {
		String sql = " insert into haipumi_user(ulogon,upwd,ualais,uimage,usex,uname,sid,uqq,ucname,ucpro,uclass,utel,uinfo,udatetime,ustatus,uwealth,urank,uglory,uvip)"
				+ " values(?,?,?,'0001.jpg',default,default,default,default,default,default,default,default,default,default,'2',default,default,default,default)";// 添加为未激活用户
		List<Object> params = new ArrayList<Object>();
		boolean flag = false;
		String md5pwd = Encrypt.md5(user.getUpwd());
		params.add(user.getUlogon());
		params.add(md5pwd);
		params.add(user.getUlogon());
		int result = 0;
		try {
			result = db.doUpdate(sql, params);
			if (result > 0) {
				// 发送邮件了
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public haipumi_user findUserInfo(String ulogon) {
		String sql = "select * from haipumi_user where ulogon=? ";
		haipumi_user backuser = null;
		List<Object> params = new ArrayList<Object>();
		List<haipumi_user> backuserlist = null;
		params.add(ulogon);
		try {
			backuserlist = db.finds(sql, params, haipumi_user.class);
			if (!backuserlist.isEmpty()) {
				backuser = backuserlist.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return backuser;
	}

	public boolean activateUser(String ulogon) {
		String sql = " update haipumi_user set ustatus=1 where ulogon=? ";
		List<Object> params = new ArrayList<Object>();
		params.add(ulogon);
		boolean flag = false;
		int result = 0;
		try {
			result = db.doUpdate(sql, params);
			if (result > 0) {
				flag = true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public haipumi_user findUnActivate(String ulogon) {
		String sql = " select * from haipumi_user where ulogon=? and ustatus=2";
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(ulogon);
		haipumi_user user = null;
		List<haipumi_user> userlist = null;
		try {
			userlist = db.finds(sql, paramsList, haipumi_user.class);
			if (!userlist.isEmpty()) {
				user = userlist.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public haipumi_user newlogin(haipumi_user user) {
		String sql = "select * from haipumi_user where ulogon=? and upwd=? ";
		List<Object> params = new ArrayList<Object>();
		params.add(user.getUlogon());
		params.add(user.getUpwd());
		haipumi_user backuser = null;
		List<haipumi_user> userlist = null;
		try {
			userlist = db.finds(sql, params, haipumi_user.class);
			if (!userlist.isEmpty()) {
				backuser = userlist.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return backuser;
	}

	@Override
	public boolean savebaseinfo(haipumi_user user) {
		String sql = null;
		List<Object> params = new ArrayList<Object>();
		if (user.getUinfo() == " " || user.getUinfo() == "") {
			sql = "update haipumi_user set ualais=?,uname=?,usex=? where uid=? ";
			params.add(user.getUalais());
			params.add(user.getUname());
			params.add(user.getUsex());
			params.add(user.getUid());
		} else {
			sql = "update haipumi_user set ualais=?,uname=?,usex=?,uinfo=? where uid=? ";
			params.add(user.getUalais());
			params.add(user.getUname());
			params.add(user.getUsex());
			params.add(user.getUinfo());
			params.add(user.getUid());
		}
		int result = 0;
		boolean flag = false;
		try {
			result = db.doUpdate(sql, params);
			if (result > 0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;
	}

	@Override
	public boolean changebackground(String uid, String sid) {
		String sql = " update haipumi_user set sid=? where uid=? ";
		List<Object> params = new ArrayList<Object>();
		params.add(sid);
		params.add(uid);
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public haipumi_skin findskin( String sid) {
		String sql = " select * from haipumi_skin where sid=? ";
		List<Object> params = new ArrayList<Object>();
		params.add(sid);
		haipumi_skin skin = null;
		List<haipumi_skin> skins = null;
		try {
			skins =db.finds(sql, params, haipumi_skin.class);
			if (!skins.isEmpty()) {
				skin=skins.get(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return skin;
	}

	@Override
	public boolean saveTel(String uid, String uqq, String utel) {
		String sql=" update haipumi_user set uqq=? ,utel=? where uid=? ";
		List<Object> params = new ArrayList<Object>();
		params.add(uqq);
		params.add(utel);
		params.add(uid);
		int result=0;
		try {
			result=db.doUpdate(sql, params);
			if(result>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

}
